<?php
namespace app\store\model;
use app\common\model\ShopClerk as ShopClerkModel;

/**
 * 店员模型
 */
class ShopClerk extends ShopClerkModel
{
    /**
     * 添加
     */
    public function add(array $data)
    {
		if(!isMobile($data['mobile'])){
			$this->error = '手机号码错误';
            return false;	
		}
		//验证手机号是否存在
		if($this->useGlobalScope(false)->where(['mobile' => $data['mobile']])->count()){
			$this->error = '手机号码被占用';
			return false;
		}
		$data['pwd'] = hema_hash($data['mobile']);
        $data['wxapp_id'] = self::$wxapp_id;
		return $this->allowField(true)->save($data);
    }

    /**
     * 编辑
     */
    public function edit($data)
    {
		//如果修改了手机号码，密码重置为新手机号码
		if($this['mobile'] != $data['mobile']){
			if(!isMobile($data['mobile'])){
				$this->error = '手机号码错误';
				return false;	
			}
			if($this->useGlobalScope(false)->where(['mobile' => $data['mobile']])->count()){
				$this->error = '手机号码被占用';
				return false;
			}
			$data['pwd'] = hema_hash($data['mobile']);
		}
        return $this->allowField(true)->save($data);
    }

    /**
     * 删除
     */
    public function remove()
    {
        return $this->delete();
    }
}
